//
//  EvaluateViewController.m
//  dangdang
//
//  Created by air on 16/1/27.
//  Copyright © 2016年 luweijie. All rights reserved.
//

#import "EvaluateViewController.h"
#import "EvaluateTableViewCell.h"
#import "DetailsViewController.h"
#import "MJRefresh.h"
#import "OrderObject.h"
#import "CompleteViewController.h"

@interface EvaluateViewController ()

@property (nonatomic, strong) UIView * backView;
@property (nonatomic, strong) UIView * starView;
@property (nonatomic, strong) UIView * evaluateView;
@property (nonatomic, strong) UIButton * sendBtn;
@property (nonatomic, strong) UIButton * serviceBtn;
@property (nonatomic, strong) NSMutableArray * sendStarButtonArray;
@property (nonatomic, strong) NSMutableArray * serviceStarButtonArray;
@property (nonatomic, strong) NSMutableArray * starButtonArray;
@property (nonatomic, strong) UIRefreshControl * refresh;
@property (nonatomic, strong) NSArray * orderCountArray;
@property (nonatomic, strong) NSMutableArray * orderDetailsArray;
@property (nonatomic, strong) NSDictionary * dic;
@property (nonatomic) BOOL pullDown;
@property (nonatomic) BOOL pullUp;
@property (nonatomic) NSInteger page;
@property (nonatomic) NSInteger starTag;
@property (nonatomic) NSInteger sendTag;
@property (nonatomic) NSInteger serviceTag;

@end

@implementation EvaluateViewController

- (instancetype)init {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"EvaluateViewController" bundle:nil];
    self = [sb instantiateViewControllerWithIdentifier:@"EvaluateViewController"];
    
    _sendStarButtonArray = [NSMutableArray new];
    _serviceStarButtonArray = [NSMutableArray new];
    _starButtonArray = [NSMutableArray new];
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self updateUI];
    
    [self initialization];
    
    [self connection];
    
    [self setupRefresh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateUI{
    _evaluateTableView.rowHeight = UITableViewAutomaticDimension;
    _evaluateTableView.estimatedRowHeight = 44.0;
    _evaluateTableView.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1.0];
}

- (void)initialization{
    _orderDetailsArray = [NSMutableArray new];
    _page = 1;
    
    _pullDown = NO;
    _pullUp = NO;
}

- (void)connection{
    NSString * token = userObject.accessToken;
    NSString * userIDString = userObject.userID;
    NSString * pageString = @"0";
    NSString * pageSizeString = @"30";
    NSString * statusString = @"6";
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:token forKey:@"token"];
    [params setObject:userIDString forKey:@"uid"];
    [params setObject:pageString forKey:@"page"];
    [params setObject:pageSizeString forKey:@"pagesize"];
    [params setObject:statusString forKey:@"status"];
    
    [self postRequestWithMethodName:@"order/get_order_list" andParams:params andSender:nil];
}

//上拉刷新
- (void)setupFooterRefresh{
    _pullUp = YES;
    
    __unsafe_unretained __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.evaluateTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
}

#pragma mark 上拉加载更多数据
- (void)loadMoreData
{
    _pullUp = YES;
    
    NSString * token = userObject.accessToken;
    NSString * userIDString = userObject.userID;
    NSString * pageString = [NSString stringWithFormat:@"%ld", (long)_page];
    NSString * pageSizeString = @"30";
    NSString * statusString = @"6";
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:token forKey:@"token"];
    [params setObject:userIDString forKey:@"uid"];
    [params setObject:pageString forKey:@"page"];
    [params setObject:pageSizeString forKey:@"pagesize"];
    [params setObject:statusString forKey:@"status"];
    
    [self postRequestWithMethodName:@"order/get_order_list" andParams:params andSender:nil];
}

/**
 *  集成下拉刷新
 */
-(void)setupRefresh
{
    self.refresh = [[UIRefreshControl alloc]init];
    //刷新图形颜色
    self.refresh.tintColor = [UIColor lightGrayColor];
    //刷新的标题
    self.refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"下拉刷新"];
    
    [self.refresh addTarget:self action:@selector(refreshStateChange:) forControlEvents:UIControlEventValueChanged];
    
    [self.evaluateTableView addSubview:self.refresh];
}

/**
 *  UIRefreshControl进入刷新状态：加载最新的数据
 */
-(void)refreshStateChange:(UIRefreshControl *)control
{
    if (control.refreshing) {
        _pullDown = YES;
        _page = 1;
        
        control.attributedTitle = [[NSAttributedString alloc]initWithString:@"正在刷新"];
        
        NSString * token = userObject.accessToken;
        NSString * userIDString = userObject.userID;
        NSString * pageString = @"0";
        NSString * pageSizeString = @"30";
        NSString * statusString = @"6";
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:token forKey:@"token"];
        [params setObject:userIDString forKey:@"uid"];
        [params setObject:pageString forKey:@"page"];
        [params setObject:pageSizeString forKey:@"pagesize"];
        [params setObject:statusString forKey:@"status"];
        
        [self postRequestWithMethodName:@"order/get_order_list" andParams:params andSender:nil];
    }
}

#pragma mark UITableViewDataSource && Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _orderDetailsArray.count;
//    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     EvaluateTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"evaluateCell" forIndexPath:indexPath];
    
    cell.evaluateButton.layer.borderWidth = 1;
    cell.evaluateButton.layer.borderColor = [[UIColor orangeColor] CGColor];
    cell.evaluateButton.layer.cornerRadius = 8;
    [cell.evaluateButton addTarget:self action:@selector(addEvaluateView:) forControlEvents:UIControlEventTouchUpInside];
    cell.evaluateButton.tag = indexPath.section;
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [[[UIColor lightGrayColor] colorWithAlphaComponent:0.5] CGColor];
    cell.layer.cornerRadius = 10;
    
    OrderObject * orderObject = [[OrderObject alloc] init];
    orderObject = _orderDetailsArray[indexPath.section];
    //    cell.firstDistanceLabel.text = [NSString stringWithFormat:@"距您%@千米", orderObject.distance];
    if ([orderObject.item_buy_location_name isEqualToString:@"<null>"]) {
        cell.buyLocationLabel.text = @"不限";
    }else{
        cell.buyLocationLabel.text = orderObject.item_buy_location_name;
    }
    cell.feeLabel.text = [NSString stringWithFormat:@"%@元", orderObject.item_price];
    cell.timeLabel.text = [NSString stringWithFormat:@"发布时间：%@", orderObject.order_time];
    //    cell.secondLabel.text = [NSString stringWithFormat:@"%@千米", orderObject.distance];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailsViewController * detailsboardVC = [[UIStoryboard storyboardWithName:@"DetailsViewController" bundle:nil] instantiateViewControllerWithIdentifier:@"DetailsViewController"];
    
    detailsboardVC.titleString = @"待评价";
    OrderObject * orderObject = [[OrderObject alloc] init];
    orderObject = _orderDetailsArray[indexPath.section];
    userObject.ordID = orderObject.order_id;
    
    [self.navigationController pushViewController:detailsboardVC animated:YES];
}

- (void)addEvaluateView:(UIButton *)button{
//    CompleteViewController * compact = [[CompleteViewController alloc] init];
//    [self.navigationController pushViewController:compact animated:YES];
    
    OrderObject * orderObject = [[OrderObject alloc] init];
    orderObject = _orderDetailsArray[button.tag];
    userObject.ordID = orderObject.order_id;
    
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    _backView.backgroundColor = [UIColor colorWithRed:10/255.0 green:10/255.0 blue:10/255.0 alpha:0.2];
    [self.view.window addSubview:_backView];
    
    _starView = [[UIView alloc] init];
    if ([UIScreen mainScreen].bounds.size.width == 375) {
        _starView.frame = CGRectMake(40, self.view.center.y - 10, self.view.frame.size.width - 80, 155);
    }else if ([UIScreen mainScreen].bounds.size.width == 414){
        _starView.frame = CGRectMake(60, self.view.center.y - 10, self.view.frame.size.width - 120, 155);
    }else{
        _starView.frame = CGRectMake(15, self.view.center.y - 10, self.view.frame.size.width - 30, 155);
    }
    _starView.backgroundColor = [UIColor whiteColor];
    _starView.layer.cornerRadius = 8;
    
    UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_starView.bounds.size.width / 2 - 42, 14, 84, 22)];
    titleLabel.text = @"星级评价";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont systemFontOfSize:19.0];
    [_starView addSubview:titleLabel];
    
    for (int i = 0; i < 5; i ++) {
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(30 + 50 * (i % 5), 52, 35, 35)];
        [button addTarget:self action:@selector(starButton:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = i;
        [_starButtonArray addObject:button];
        [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
        [_starView addSubview:button];
    }
    
    UIView * lineB = [[UIView alloc] initWithFrame:CGRectMake(0, 109, _starView.bounds.size.width, 1)];
    lineB.backgroundColor = [UIColor lightGrayColor];
    [_starView addSubview:lineB];
    
    UIView * lineC = [[UIView alloc] initWithFrame:CGRectMake(_starView.bounds.size.width / 2, 110, 1, 45)];
    lineC.backgroundColor = [UIColor lightGrayColor];
    [_starView addSubview:lineC];
    
    UIButton * cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 110, _starView.bounds.size.width / 2, 45)];
    [cancelButton addTarget:self action:@selector(cancelOrConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
    cancelButton.tag = 1;
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [_starView addSubview:cancelButton];
    
    UIButton * confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(_starView.bounds.size.width / 2 + 1, 110, _starView.bounds.size.width / 2, 45)];
    [confirmButton addTarget:self action:@selector(cancelOrConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.tag = 2;
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor colorWithRed:60/255.0 green:120/255.0 blue:250/255.0 alpha:1.0] forState:UIControlStateNormal];
    confirmButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [_starView addSubview:confirmButton];
    
    [_backView addSubview:_starView];
}

- (void)starButton:(UIButton *)starButton {
    if (_sendBtn == nil){
        for (int i = 0; i <= starButton.tag; i ++) {
            UIButton * button = _starButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
        }
        _sendBtn = starButton;
        _starTag = starButton.tag + 1;
    }
    else if (_sendBtn !=nil && _sendBtn == starButton){
        for (int i = 0; i <= starButton.tag; i ++) {
            UIButton * button = _starButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
        }
        _sendBtn = nil;
        _starTag = 0;
    }
    else if (_sendBtn!= starButton && _sendBtn!=nil){
        for (int i = 0; i <= _starButtonArray.count - 1; i ++) {
            if (i <= starButton.tag) {
                UIButton * button = _starButtonArray[i];
                [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
            }else{
                UIButton * button = _starButtonArray[i];
                [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
            }
        }
        _sendBtn = starButton;
        _starTag = starButton.tag + 1;
    }
}

- (void)sendStarButton:(UIButton *)sendButton {
    if (_sendBtn == nil){
        for (int i = 0; i <= sendButton.tag; i ++) {
            UIButton * button = _sendStarButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
        }
        _sendBtn = sendButton;
        _sendTag = sendButton.tag + 1;
    }
    else if (_sendBtn !=nil && _sendBtn == sendButton){
        for (int i = 0; i <= sendButton.tag; i ++) {
            UIButton * button = _sendStarButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
        }
        _sendBtn = nil;
        _sendTag = 0;
    }
    else if (_sendBtn!= sendButton && _sendBtn!=nil){
        for (int i = 0; i <= _sendStarButtonArray.count - 1; i ++) {
            if (i <= sendButton.tag) {
                UIButton * button = _sendStarButtonArray[i];
                [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
            }else{
                UIButton * button = _sendStarButtonArray[i];
                [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
            }
        }
        _sendBtn = sendButton;
        _sendTag = sendButton.tag + 1;
    }
}

- (void)serviceStarButton:(UIButton *)serviceButton {
    if (_serviceBtn == nil){
        for (int i = 0; i <= serviceButton.tag; i ++) {
            UIButton * button = _serviceStarButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
        }
        [serviceButton setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
        _serviceBtn = serviceButton;
        _serviceTag = serviceButton.tag + 1;
    }
    else if (_serviceBtn !=nil && _serviceBtn == serviceButton){
        
        for (int i = 0; i <= serviceButton.tag; i ++) {
            UIButton * button = _serviceStarButtonArray[i];
            [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
        }
        [serviceButton setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
        _serviceBtn = nil;
        _serviceTag = 0;
    }
    else if (_serviceBtn!= serviceButton && _serviceBtn!=nil){
        for (int i = 0; i <= _serviceStarButtonArray.count - 1; i ++) {
            if (i <= serviceButton.tag) {
                UIButton * button = _serviceStarButtonArray[i];
               [button setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
            }else{
                UIButton * button = _serviceStarButtonArray
                [i];
                [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
            }
        }
        [serviceButton setImage:[UIImage imageNamed:@"hk_star_down2.png"] forState:UIControlStateNormal];
        _serviceBtn = serviceButton;
        _serviceTag = serviceButton.tag + 1;
    }
}

- (void)cancelOrConfirmButton:(UIButton *)button{
    if (button.tag == 1) {
        _starTag = 0;
        [_backView removeFromSuperview];
    }else{
        if (_starTag == 0) {
            [self alertViewShowWithAlertString:@"请选择星级评价"];
            return;
        }
        [_starView removeFromSuperview];
        _evaluateView = [[UIView alloc] init];
        if ([UIScreen mainScreen].bounds.size.width == 375) {
            _evaluateView.frame = CGRectMake(40, self.view.center.y - 10, self.view.frame.size.width - 80, 195);
        }else if ([UIScreen mainScreen].bounds.size.width == 414){
            _evaluateView.frame = CGRectMake(60, self.view.center.y - 10, self.view.frame.size.width - 120, 195);
        }else{
            _evaluateView.frame = CGRectMake(15, self.view.center.y - 10, self.view.frame.size.width - 30, 195);
        }
        _evaluateView.backgroundColor = [UIColor whiteColor];
        _evaluateView.layer.cornerRadius = 8;
        
        UIView * lineA = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _evaluateView.bounds.size.width, 1)];
        lineA.backgroundColor = [UIColor lightGrayColor];
        [_evaluateView addSubview:lineA];
        
        UILabel * titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(_evaluateView.bounds.size.width / 2 - 21, 14, 42, 22)];
        titleLabel.text = @"评价";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.font = [UIFont systemFontOfSize:19.0];
        [_evaluateView addSubview:titleLabel];
        
        UILabel * sendLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 70, 95, 21)];
        sendLabel.text = @"派送速度：";
        sendLabel.textColor = [UIColor darkGrayColor];
        sendLabel.font = [UIFont systemFontOfSize:19.0];
        [_evaluateView addSubview:sendLabel];
        
        UILabel * serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(22, 107, 95, 21)];
        serviceLabel.text = @"服务态度：";
        serviceLabel.textColor = [UIColor darkGrayColor];
        serviceLabel.font = [UIFont systemFontOfSize:19.0];
        [_evaluateView addSubview:serviceLabel];
        
        for (int i = 0; i < 10; i ++) {
            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(112 + 33 * (i % 5), 62 + 37 * (i / 5), 30, 30)];
            if (i < 5) {
                [button addTarget:self action:@selector(sendStarButton:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = i;
                [_sendStarButtonArray addObject:button];
            }else{
                [button addTarget:self action:@selector(serviceStarButton:) forControlEvents:UIControlEventTouchUpInside];
                button.tag = i - 5;
                [_serviceStarButtonArray addObject:button];
            }
            [button setImage:[UIImage imageNamed:@"hk_star_up2.png"] forState:UIControlStateNormal];
            [_evaluateView addSubview:button];
        }
        
        UIView * lineB = [[UIView alloc] initWithFrame:CGRectMake(0, 149, _evaluateView.bounds.size.width, 1)];
        lineB.backgroundColor = [UIColor lightGrayColor];
        [_evaluateView addSubview:lineB];
        
        UIView * lineC = [[UIView alloc] initWithFrame:CGRectMake(_evaluateView.bounds.size.width / 2, 150, 1, 45)];
        lineC.backgroundColor = [UIColor lightGrayColor];
        [_evaluateView addSubview:lineC];
        
        UIButton * cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 150, _evaluateView.bounds.size.width / 2, 45)];
        [cancelButton addTarget:self action:@selector(cancelAndConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
        cancelButton.tag = 1;
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [_evaluateView addSubview:cancelButton];
        
        UIButton * confirmButton = [[UIButton alloc] initWithFrame:CGRectMake(_evaluateView.bounds.size.width / 2 + 1, 150, _evaluateView.bounds.size.width / 2, 45)];
        [confirmButton addTarget:self action:@selector(cancelAndConfirmButton:) forControlEvents:UIControlEventTouchUpInside];
        confirmButton.tag = 2;
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor colorWithRed:60/255.0 green:120/255.0 blue:250/255.0 alpha:1.0] forState:UIControlStateNormal];
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:20.0];
        [_evaluateView addSubview:confirmButton];
        
        [_backView addSubview:_evaluateView];
    }
    [_starButtonArray removeAllObjects];
}

- (void)cancelAndConfirmButton:(UIButton *)button{
    if (button.tag == 1) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }else{
//        NSLog(@"sendtag = %ld, ser = %ld", _sendTag, _serviceTag);
        if (_sendTag == 0) {
            [self alertViewShowWithAlertString:@"请选择派送速度星级评价"];
            return;
        }
        if (_serviceTag == 0) {
            [self alertViewShowWithAlertString:@"请选择服务态度星级评价"];
            return;
        }
        NSString * token = userObject.accessToken;
        NSString * userIDString = userObject.userID;
        NSString * orderID = userObject.ordID;
        NSString * content = @"";
        NSString * stars = [NSString stringWithFormat:@"%ld", (long)_starTag];
        NSString * stars1 = [NSString stringWithFormat:@"%ld", (long)_sendTag];
        NSString * stars2 = [NSString stringWithFormat:@"%ld", (long)_serviceTag];
//        NSLog(@"1 = %@, 2 = %@, 3 = %ld", userIDString, orderID, _serviceTag);
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:token forKey:@"token"];
        [params setObject:userIDString forKey:@"uid"];
        [params setObject:orderID forKey:@"order_id"];
        [params setObject:content forKey:@"content"];
        [params setObject:stars forKey:@"stars"];
        [params setObject:stars1 forKey:@"stars1"];
        [params setObject:stars2 forKey:@"stars2"];
        
        [self postRequestWithMethodName:@"order/comment" andParams:params andSender:nil];
        [LoadingView showInView:self.view image:nil info:@"请稍候..." animated:YES modeled:YES autoHide:NO];
    }
    _starTag = 0;
    _sendTag = 0;
    _serviceTag = 0;
    [_sendStarButtonArray removeAllObjects];
    [_serviceStarButtonArray removeAllObjects];
    [_backView removeFromSuperview];
}

//网络连接返回数据
- (void)requestSuccessedWithResult:(id)result NSURLSessionTask:(NSURLSessionTask *)task sender:(id)sender{
    
    NSString * urlString = [NSString stringWithFormat:@"%@", task.currentRequest.URL];
    
    if ([result isKindOfClass:[NSDictionary class]]) {
        NSString * code = [NSString stringWithFormat:@"%@", [result objectForKey:@"retcode"]];
        if ([urlString isEqualToString:[NSString stringWithFormat:@"%@%@", URLDoman, @"order/get_order_list"]]){
            if ([code isEqualToString:@"0"]) {
                NSArray * arrayCount = [result objectForKey:@"retmsg"];
                if (arrayCount.count == 0 && _pullDown == YES) {
                    self.refresh.attributedTitle = [[NSAttributedString alloc] initWithString:@"全部加载完毕"];
                    [self.refresh endRefreshing];
                    _pullDown = NO;
                }else if (!(arrayCount.count == 0) && _pullDown == YES){
                    _orderDetailsArray = [NSMutableArray new];
                    _orderCountArray = [result objectForKey:@"retmsg"];
                    
                    for (int i = 0; i < _orderCountArray.count; i ++) {
                        OrderObject * orderObject = [[OrderObject alloc] init];
                        _dic = _orderCountArray[i];
                        orderObject.city_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"city_id"]];
                        orderObject.distance = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"distance"]];
                        orderObject.exp_fee = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_fee"]];
                        orderObject.exp_uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_uid"]];
                        orderObject.is_commented = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"is_commented"]];
                        orderObject.item_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_name"]];
                        orderObject.item_price = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_price"]];
                        orderObject.item_weight = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_weight"]];
                        orderObject.location_lat = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lat"]];
                        orderObject.location_lng = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lng"]];
                        orderObject.location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_name"]];
                        orderObject.order_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_id"]];
                        orderObject.order_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_serial"]];
                        orderObject.order_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_time"]];
                        orderObject.pay_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_serial"]];
                        orderObject.pay_status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_status"]];
                        orderObject.pay_type = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_type"]];
                        orderObject.reach_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time"]];
                        orderObject.reach_time_final = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time_final"]];
                        orderObject.remark = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"remark"]];
                        orderObject.status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"status"]];
                        orderObject.uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"uid"]];
                        orderObject.item_buy_location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_buy_location_name"]];
                        
                        [_orderDetailsArray addObject:orderObject];
                    }
                    [self.refresh endRefreshing];
                    
                    [_evaluateTableView reloadData];
                    
                    [self setupFooterRefresh];
                    
                    _pullDown = NO;
                }else if (!(arrayCount.count == 0) && _pullUp == YES){
                    NSLog(@"2222222");
                    _orderCountArray = [result objectForKey:@"retmsg"];
                    for (int i = 0; i < _orderCountArray.count; i ++) {
                        OrderObject * orderObject = [[OrderObject alloc] init];
                        _dic = _orderCountArray[i];
                        orderObject.city_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"city_id"]];
                        orderObject.distance = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"distance"]];
                        orderObject.exp_fee = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_fee"]];
                        orderObject.exp_uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_uid"]];
                        orderObject.is_commented = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"is_commented"]];
                        orderObject.item_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_name"]];
                        orderObject.item_price = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_price"]];
                        orderObject.item_weight = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_weight"]];
                        orderObject.location_lat = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lat"]];
                        orderObject.location_lng = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lng"]];
                        orderObject.location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_name"]];
                        orderObject.order_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_id"]];
                        orderObject.order_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_serial"]];
                        orderObject.order_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_time"]];
                        orderObject.pay_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_serial"]];
                        orderObject.pay_status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_status"]];
                        orderObject.pay_type = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_type"]];
                        orderObject.reach_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time"]];
                        orderObject.reach_time_final = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time_final"]];
                        orderObject.remark = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"remark"]];
                        orderObject.status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"status"]];
                        orderObject.uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"uid"]];
                        orderObject.item_buy_location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_buy_location_name"]];
                        
                        [_orderDetailsArray addObject:orderObject];
                    }
                    _page ++;
                    [_evaluateTableView.mj_footer endRefreshing];
                    [_evaluateTableView reloadData];
                    
                    _pullUp = NO;
                }else if (arrayCount.count == 0 && _pullUp == YES){
                    [_evaluateTableView.mj_footer endRefreshingWithNoMoreData];
                    
                    _pullUp = NO;
                }else if (!(arrayCount.count == 0)){
                    NSLog(@"111111");
                    _orderCountArray = [result objectForKey:@"retmsg"];
                    for (int i = 0; i < _orderCountArray.count; i ++) {
                        OrderObject * orderObject = [[OrderObject alloc] init];
                        _dic = _orderCountArray[i];
                        orderObject.city_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"city_id"]];
                        orderObject.distance = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"distance"]];
                        orderObject.exp_fee = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_fee"]];
                        orderObject.exp_uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"exp_uid"]];
                        orderObject.is_commented = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"is_commented"]];
                        orderObject.item_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_name"]];
                        orderObject.item_price = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_price"]];
                        orderObject.item_weight = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_weight"]];
                        orderObject.location_lat = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lat"]];
                        orderObject.location_lng = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_lng"]];
                        orderObject.location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"location_name"]];
                        orderObject.order_id = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_id"]];
                        orderObject.order_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_serial"]];
                        orderObject.order_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"order_time"]];
                        orderObject.pay_serial = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_serial"]];
                        orderObject.pay_status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_status"]];
                        orderObject.pay_type = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"pay_type"]];
                        orderObject.reach_time = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time"]];
                        orderObject.reach_time_final = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"reach_time_final"]];
                        orderObject.remark = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"remark"]];
                        orderObject.status = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"status"]];
                        orderObject.uid = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"uid"]];
                        orderObject.item_buy_location_name = [NSString stringWithFormat:@"%@", [_dic objectForKey:@"item_buy_location_name"]];
                        
                        [_orderDetailsArray addObject:orderObject];
                    }
                    [_evaluateTableView reloadData];
                    
                    [self setupFooterRefresh];
                }else if (arrayCount.count == 0){
                    [_evaluateTableView reloadData];
                }
            }else{
                if (_pullUp == YES) {
                    [_evaluateTableView.mj_footer endRefreshing];
                }else if (_pullDown == YES){
                    [self.refresh endRefreshing];
                }
            }
        }else if ([urlString isEqualToString:[NSString stringWithFormat:@"%@%@", URLDoman, @"order/comment"]]){
            if ([code isEqualToString:@"0"]) {
                _pullUp = NO;
                _orderDetailsArray = [NSMutableArray new];
                [self connection];
            }
        }
    }
}

- (void)requestFailureWithNSURLSessionTask:(NSURLSessionTask *)task error:(NSError *)error sender:(id)sender
{
    if (_pullUp == YES) {
        [_evaluateTableView.mj_footer endRefreshing];
    }else if (_pullDown == YES){
        [self.refresh endRefreshing];
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
